package com.biancheng.biancheng.biancheng3;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class 两个数组的交集 {

    public static void main(String[] args) {
        两个数组的交集 ff = new 两个数组的交集();
        int[] re = ff.intersection(new int[]{-1,0,1,2,-1,-4},new int[]{-1,-4});
        System.out.println(Arrays.toString(re));
    }
    public int[] intersection(int[] nums1, int[] nums2) {
        if (nums1 == null || nums1.length == 0 || nums2 == null || nums2.length == 0) {
            return new int[0];
        }
        Set<Integer> hash1 = new HashSet<>();
        Set<Integer> hash2 = new HashSet<>();
        for(int i : nums1) {
            hash1.add(i);
        }
        for(int i : nums2) {
            if(hash1.contains(i)) {
                hash2.add(i);
            }
        }
        int[] res = new int[hash2.size()];
        int index = 0;
        for(int i : hash2) {
            res[index++] = i;
        }
        return res;
    }

}
